package com.neo.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class JsoupUtlis {
    private static Map<Integer, String> cssMap = new HashMap<Integer, String>();

    static {
        cssMap.put(1, "provincetr");// 省
        cssMap.put(2, "citytr");// 市
        cssMap.put(3, "countytr");// 县
        cssMap.put(4, "towntr");// 镇
        cssMap.put(5, "villagetr");// 村
    }

    public static void main(String[] args) {
        int level = 1;


        // 获取全国各个省级信息
        Document connect = connect("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2019/");
        Elements rowProvince = connect.select("tr." + cssMap.get(level));
        for (Element provinceElement : rowProvince)// 遍历每一行的省份城市
        {
            Elements select = provinceElement.select("td");
            for (Element province : select)// 每一个省份(四川省)
            {
                // 获得上级链接
                // log.info(province.baseUri());
                // 获得标签内文本值
                log.info(province.text());
                // 获得某个标签内的 属性值 下方获得a标签内的href值
                String href = province.baseUri() + province.select("a").attr("href");
                log.info(href);
                // 获得下级链接值

                try {
                    parseNextLevel(href, level + 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private static void parseNextLevel(String url, int level) throws IOException {
        Document doc = connect(url);
        if (doc != null) {
            Elements newsHeadlines = doc.select("tr." + cssMap.get(level));//
            // 获取表格的一行数据
            for (Element element : newsHeadlines) {
//                printInfo(element, level + 1);
                log.info(element.text() + "");
                Elements select = element.select("a");
                // 在递归调用的时候，这里是判断是否是村一级的数据，村一级的数据没有a标签
                if (select.size() != 0) {
                    log.info(element.baseUri().substring(0, element.baseUri().lastIndexOf("/")+1));
                    log.info(element.select("a").attr("href"));
                    String href = element.baseUri().substring(0, element.baseUri().lastIndexOf("/")+1)  + element.select("a").attr("href");
                    parseNextLevel(href, level + 1);
                }
            }
        }
    }


    private static Document connect(String url) {
        if (url == null || url.isEmpty()) {
            throw new IllegalArgumentException("The input url('" + url + "') is invalid!");
        }
        try {
            return Jsoup.parse(new URL(url).openStream(), "GBK", url);

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
